Completed
Push — master ( 2281e8...8b163e )
by Justin
01:35
created

Note.isInstance   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
var ExtensibleData = require('./ExtensibleData'),
2
    Attribution = require('./Attribution'),
3
    utils = require('./utils');
4
5
/**
6
 * A note about a resource.
7
 * 
8
 * @constructor
9
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
10
 */
11
var Note = function(json){
12
  
13
  // Protect against forgetting the new keyword when calling the constructor
14
  if(!(this instanceof Note)){
15
    return new Note(json);
16
  }
17
  
18
  // If the given object is already an instance then just return it. DON'T copy it.
19
  if(Note.isInstance(json)){
20
    return json;
21
  }
22
  
23
  ExtensibleData.call(this, json);
24
  
25
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
26
    this.setLang(json.lang);
27
    this.setSubject(json.subject);
28
    this.setText(json.text);
29
    this.setAttribution(json.attribution);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
30
  }
31
};
32
33
Note.prototype = Object.create(ExtensibleData.prototype);
34
35
Note._gedxClass = Note.prototype._gedxClass = 'GedcomX.Note';
36
37
/**
38
 * Check whether the given object is an instance of this class.
39
 * 
40
 * @param {Object} obj
41
 * @returns {Boolean}
42
 */
43
Note.isInstance = function(obj){
44
  return utils.isInstance(obj, this._gedxClass);
45
};
46
47
/**
48
 * Get the language identifier.
49
 * 
50
 * @returns {String} lang
51
 */
52
Note.prototype.getLang = function(){
53
  return this.lang;
54
};
55
56
/**
57
 * Set the language identifier.
58
 * 
59
 * @param {String} lang
60
 * @returns {Note} This instance.
61
 */
62
Note.prototype.setLang = function(lang){
63
  this.lang = lang;
64
  return this;
65
};
66
67
/**
68
 * Get the subject.
69
 */
70
Note.prototype.getSubject = function(){
71
  return this.subject;
72
};
73
74
/**
75
 * Set the subject.
76
 * 
77
 * @param {String} subject
78
 * @returns {Note} This note.
79
 */
80
Note.prototype.setSubject = function(subject){
81
  this.subject = subject;
82
  return this;
83
};
84
85
/**
86
 * Get the text.
87
 * 
88
 * @returns {String} text
89
 */
90
Note.prototype.getText = function(){
91
  return this.text;
92
};
93
94
/**
95
 * Set the text.
96
 * 
97
 * @param {String} text
98
 * @returns {Note} This note.
99
 */
100
Note.prototype.setText = function(text){
101
  this.text = text;
102
  return this;
103
};
104
105
/**
106
 * Get the attribution.
107
 * 
108
 * @returns {Attribution}
109
 */
110
Note.prototype.getAttribution = function(){
111
  return this.attribution;
112
};
113
114
/**
115
 * Set the attribution
116
 * 
117
 * @param {Object|Attribution} attribution
118
 * @returns {Note} This instance.
119
 */
120
Note.prototype.setAttribution = function(attribution){
121
  if(attribution){
122
    this.attribution = new Attribution(attribution);
123
  }
124
  return this;
125
};
126
127
/**
128
 * Export the object as JSON
129
 * 
130
 * @return {Object} JSON object
131
 */
132
Note.prototype.toJSON = function(){
133
  var json = ExtensibleData.prototype.toJSON.call(this);
134
  
135
  if(this.lang){
136
    json.lang = this.lang;
137
  }
138
  
139
  if(this.subject){
140
    json.subject = this.subject;
141
  }
142
  
143
  if(this.text){
144
    json.text = this.text;
145
  }
146
  
147
  if(this.attribution){
148
    json.attribution = this.attribution.toJSON();
149
  }
150
  
151
  return json;
152
};
153
154
module.exports = Note;